from django.http.response import JsonResponse
from .models import *
# Create your views here.
def new_get(request):
    news = New.objects.all()
    data = [
        {
            'id':i.id,
            'name':i.name,
            'content':i.content
        }
        for i in news
    ]
    return JsonResponse(data,safe=False)
def new_post(request):
    id = request.POST.get('id')
    name = request.POST.get('name')
    content = request.POST.get('content')
    if not all([id,name,content]):
        return JsonResponse({'msg':'缺少参数'},status=400)
    try:
        New.objects.create(id=id,name=name,content=content)
    except Exception as e:
        print(e)
        return JsonResponse({'msg':'添加失败'},status=500)
    return JsonResponse({'msg':'添加成功'},status=201)
def new_detial(request,pk):
    try:
        s = New.objects.all(id=pk)
    except New.DoesNotExist:
        return JsonResponse({'msg':'NOT FOUND'},status=404)
    data = {
        'id':s.id,
        'name':s.name,
        'content':s.content
    }
    return JsonResponse(data,status=201)
